package com.min.star.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.min.star.mapper.CurriculumMapper;
import com.min.star.model.Curriculum;
import com.min.star.model.Grade;
import com.min.star.service.ICurriculumService;

@Service
public class CurriculumServiceImpl implements ICurriculumService {

	@Autowired
	private CurriculumMapper curriculumMapper;

	@Override
	public List<Curriculum> selectCurriculumList() {
		return this.curriculumMapper.selectList(null);
	}

	@Override
	public Page<Curriculum> selectPage(Page<Curriculum> page, Curriculum curriculum) {
		QueryWrapper<Curriculum> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				// 如果出版社名称不为空，则对出版社进行模糊查询，是column列的值为 val
				.like(!StringUtils.isEmpty(curriculum.getCurName()), Curriculum::getCurName, curriculum.getCurName())

				.eq(!StringUtils.isEmpty(curriculum.getCurCredit()), Curriculum::getCurCredit,
						curriculum.getCurCredit())
				.like(!StringUtils.isEmpty(curriculum.getCurTimes()), Curriculum::getCurTimes, curriculum.getCurTimes())
				.like(!StringUtils.isEmpty(curriculum.getCurLocation()), Curriculum::getCurLocation,
						curriculum.getCurLocation());
		Page<Curriculum> p = curriculumMapper.selectPage(page, queryWrapper);
		return p;
	}

	@Override
	public Page<Curriculum> findPage(Page<Curriculum> page, Curriculum curriculum) {
		return this.curriculumMapper.findPage(page, curriculum);
//		return this.curriculumMapper.selectPage(page, null);
	}

	@Override
	public int add(Curriculum curriculum) {
		return this.curriculumMapper.insert(curriculum);

	}

	@Override
	public int delete(String ids) {

		return this.curriculumMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
	}

	@Override
	public boolean checkCurriculumTimeExist4Add(Curriculum curriculum) {
		QueryWrapper<Curriculum> queryWrapper = new QueryWrapper<>();
		// 参数1：Model的get方法，形式是Model::get方法名 where publisher_name = '清华大学出版社'
		queryWrapper.lambda().eq(Curriculum::getCurTimes, curriculum.getCurTimes());
		queryWrapper.lambda().eq(Curriculum::getCurLocation, curriculum.getCurLocation());
		// queryWrapper作为查询条件传递给selectCount方法
		Integer count = this.curriculumMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}

	@Override
	public Curriculum save(Curriculum curriculum) {
		this.curriculumMapper.insert(curriculum);
		Long id = curriculum.getId();
		System.out.println("id in service =============" + id);
		return this.curriculumMapper.selectById(id);
	}

	@Override
	public boolean checkCurriculumTimeExist4Edit(Curriculum curriculum) {
		QueryWrapper<Curriculum> queryWrapper = new QueryWrapper<>();
		// 参数1：Model的get方法，形式是Model::get方法名 where publisher_name = '清华大学出版社'
		queryWrapper.lambda().eq(Curriculum::getCurTimes, curriculum.getCurTimes());
		queryWrapper.lambda().eq(Curriculum::getCurLocation, curriculum.getCurLocation());
		queryWrapper.lambda().ne(Curriculum::getId, curriculum.getId());
		// queryWrapper作为查询条件传递给selectCount方法
		Integer count = this.curriculumMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}

	@Override
	public Curriculum update(Curriculum curriculum) {
		int ret = this.curriculumMapper.updateById(curriculum);
		return this.curriculumMapper.selectById(curriculum.getId());
	}

}
